package com.ruoyi.iot.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.iot.domain.IotRulesTrigger;
import com.ruoyi.iot.service.IIotRulesTriggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 规则触发器Controller
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@RestController
@RequestMapping("/iot/trigger")
public class IotRulesTriggerController extends BaseController {
    @Autowired
    private IIotRulesTriggerService iotRulesTriggerService;

    /**
     * 查询规则触发器列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IotRulesTrigger iotRulesTrigger) {
        startPage();
        List<IotRulesTrigger> list = iotRulesTriggerService.selectIotRulesTriggerList(iotRulesTrigger);
        return getDataTable(list);
    }

    /**
     * 导出规则触发器列表
     */
    @Log(title = "规则触发器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotRulesTrigger iotRulesTrigger) {
        List<IotRulesTrigger> list = iotRulesTriggerService.selectIotRulesTriggerList(iotRulesTrigger);
        ExcelUtil<IotRulesTrigger> util = new ExcelUtil<IotRulesTrigger>(IotRulesTrigger.class);
        util.exportExcel(response, list, "规则触发器数据");
    }

    /**
     * 获取规则触发器详细信息
     */
    @GetMapping(value = "/{triggerId}")
    public AjaxResult getInfo(@PathVariable("triggerId") Long triggerId) {
        return success(iotRulesTriggerService.selectIotRulesTriggerByTriggerId(triggerId));
    }

    /**
     * 新增规则触发器
     */
    @Log(title = "规则触发器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotRulesTrigger iotRulesTrigger) {
        return toAjax(iotRulesTriggerService.insertIotRulesTrigger(iotRulesTrigger));
    }

    /**
     * 修改规则触发器
     */
    @Log(title = "规则触发器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotRulesTrigger iotRulesTrigger) {
        return toAjax(iotRulesTriggerService.updateIotRulesTrigger(iotRulesTrigger));
    }

    /**
     * 删除规则触发器
     */
    @Log(title = "规则触发器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{triggerIds}")
    public AjaxResult remove(@PathVariable Long[] triggerIds) {
        return toAjax(iotRulesTriggerService.deleteIotRulesTriggerByTriggerIds(triggerIds));
    }

    /**
     * 根据规则ID获取规则触发器
     */
    @GetMapping("/getRuleTriggerByRuleId/{ruleId}")
    public AjaxResult getRuleTriggerByRuleId(@PathVariable("ruleId") Long ruleId) {
        return success(iotRulesTriggerService.selectIotRulesTriggerByRuleId(ruleId));
    }
}
